const medica = require('../../utils/medica.common.js')
import api from '../../../api/index'
const md5 = require("../../utils/md5");
Page({
  data: {
    value: '',  
    show: false,
    show1: false,
    show2: false,
    startDate: '',
    endDate: '',
    minHour: 0,
    maxHour: 24,
    toolbarshow: false
  },
  onLoad(options) {
  },
  onShow() {
    this.get()
  },
  async get() {
    console.log('获取储存绑定设备名称列表--------1111---------');
  this.setData({
   bindDevice: []
  })
  try {
   this.setData({
    loading: true
   })
    const res = await api.post('/api/user/storage/get', {
   data: {
     'key': 'setUptime'
   }
    })
    console.log(res[0].value); 
    if(res[0].value!=="") {
      console.log(res);
      const listData = JSON.parse(res[0].value)[0]
      console.log(listData);
      console.log(listData.startDate);
      this.setData({
        startDate: listData.startDate,
        endDate: listData.endDate,
        show: true,
        show1: true
      });
    } else {
      this.setData({
        show: true
      });
    }
  } catch (e) {
    console.log('get->', e)
    return null
  }
  },
  // 储存
	async doSaveTime(result, type) {
		try {
		  await api.post('/api/user/storage/set', {
				data: {
					'key': 'setUptime',
					'value': result
				}
				})
				wx.showToast({
					title: `${type}成功`,
					icon: 'none',
					duration: 3000
				})
				this.setData({ showDevice: false });
				this.get()
			} catch (e) {
				console.log('put->', e)
			}
	  },

  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      show1: true,
      show2: false
    });
  },
  onChange2(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
    this.setData({
      show1: false,
      show2: true
    });
  },
  onInput(event) {
    console.log(event);
    this.setData({
      startDate: event.detail,
    });
  },
  onInput2(event) {
    console.log(event);
    this.setData({
      endDate: event.detail,
    });
  },
  setUpTime () {
    if(!this.data.startDate) {
      wx.showToast({
        title: `请选择开始时间`,
        icon: 'none',
        duration: 3000
      })
    }
    if(!this.data.endDate) {
      wx.showToast({
        title: `请选择结束时间`,
        icon: 'none',
        duration: 3000
      })
    }
    console.log(this.data.startDate,'开始时间');
    const startDate = this.data.startDate
    const stList = startDate.split(':')
    console.log(this.data.endDate,'结束时间');
    const endDate = this.data.endDate
    const duration = this.calculateDuration(startDate, endDate);
    console.log(duration);
    let result = [{startDate: startDate,endDate: endDate}]
    result = JSON.stringify(result)
		this.doSaveTime(result, '储存')
    let pages=getCurrentPages();  //获取当前页面
    let beforePage=pages[pages.length-2]  //获取上一个页面的实例
    if(beforePage.setMonitorTime) {
      beforePage.setMonitorTime(Number(stList[0]), Number(stList[1]), duration)
    }
  },
  calculateDuration(start, end) {
    const [startHour, startMinute] = start.split(':').map(Number);
    const [endHour, endMinute] = end.split(':').map(Number);
    let duration = 0;
    if (startHour < endHour) {
      duration = (endHour - startHour) * 60 + (endMinute - startMinute);
    } else if (startHour === endHour) {
      duration = endMinute - startMinute;
    } else {
      duration = (24 - startHour + endHour) * 60 + (endMinute - startMinute);
    }
    return Math.abs(duration);
  }
});
